package com.neusoft.news.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Servlet 基类，用于统一处理编码格式
 *
 * @author wh
 * @ClassName: BaseServlet
 * @Description: 统一处理编码格式
 * @date 2021年5月22日
 * @since JDK 1.8
 */
public class BaseServlet extends HttpServlet {

    private final String code = "UTF-8";

    private void preEncoding(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding(code);
        resp.setCharacterEncoding(code);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        preEncoding(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        preEncoding(req, resp);
    }

}
